﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using LitJson;

namespace ZGameFramework.Config
{
    public class ConfigCfgData
    {
        /// <summary>
        /// 加载配置文件
        /// </summary>
        /// <returns></returns>
        public virtual bool Load() { return false; }

        /// <summary>
        /// 加载配置文件
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="dict"></param>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public static bool LoadCfg<T>( Dictionary<int,T> dict, string fileName )where T:ConfigCfg
        {
            if (null == dict) {
                dict = new Dictionary<int, T>();
            }

            // 读取文件 更新 
            // 用json 实现
            dict.Clear();

            string text = CFileSystem.ReadStringFile( "Cfg/"+ fileName + ".json", Application.streamingAssetsPath );
            JsonData tab = JsonMapper.ToObject(text);
            JsonData data = tab["data"];
            foreach (JsonData jd in data)
            {
                T item =  JsonMapper.ToObject<T>( jd.ToJson() );
                dict.Add(item.m_Id, item);
            }

            return true;
        }
    }

}